package com.dofun.locationservice.base;

import android.support.annotation.UiThread;

import java.lang.ref.WeakReference;

/**
 * Created by G on 2018/8/6.
 */

public abstract class BasePresenter<T> implements IBasePresenter<T> {

    /*View弱引用*/
    protected WeakReference<T> mViewRef;


    //绑定
    @UiThread
    @Override
    public void attachView(T view) {
        mViewRef = new WeakReference<>(view);
        init();
    }

    protected abstract void init();

    //解绑
    @UiThread
    @Override
    public void detachView() {
        if (mViewRef != null) {
            mViewRef.clear();
            mViewRef = null;
        }
    }


    /**
     * 获取view
     *
     * @return
     */
    @UiThread
    public T getView() {
        return mViewRef == null ? null : mViewRef.get();
    }

    /**
     * 判断View是否已经添加
     *
     * @return
     */
    @UiThread
    public boolean isViewAttached() {
        return mViewRef != null && mViewRef.get() != null;
    }



}
